package com.cakeclient;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.actionbarsherlock.app.SherlockFragment;
import com.cakeclient.Model.Address;
import com.cakeclient.Model.Client;
import com.cakeclient.Model.Place;

public class ClientRegisterFragment2 extends SherlockFragment{

    private Client client;
    private Address address = new Address();

    public ClientRegisterFragment2(Client client) {
        this.client = client;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.client_register_form2,container,false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        Button buttonFinish = (Button) getSherlockActivity().findViewById(R.id.client_register_finis_button);
        
        buttonFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText place1EditText = (EditText) getSherlockActivity().findViewById(R.id.client_register_place_ligne1_edittext);
                EditText place2EditText = (EditText) getSherlockActivity().findViewById(R.id.client_register_place_ligne2_edittext);
                EditText cityEditText = (EditText) getSherlockActivity().findViewById(R.id.client_register_place_city_edittext);
                EditText postalCodeEditText = (EditText) getSherlockActivity().findViewById(R.id.client_register_place_postalcode_edittext);

                address.setLine1(place1EditText.getText().toString());
                address.setLine2(place2EditText.getText().toString());
                address.setCity(cityEditText.getText().toString());
                address.setPostalCode(postalCodeEditText.getText().toString());

                Place clientPlace = new Place(address);
                clientPlace.saveToDatabase(getSherlockActivity());
                client.setLivingPlace(clientPlace);
                client.saveToDatabase(getSherlockActivity());

                SyncTaskManager.sendClient(getSherlockActivity(), client);

                ((MainParametersActivity) getSherlockActivity()).LaunchAccountsFragment();
            }
        });



    }



}
